package com.arpa.oms.controller;

import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.OmsReplacedGoodsDTO;
import com.arpa.oms.domain.vo.OmsReplacedGoodsVO;
import com.arpa.oms.service.IOmsReplacedGoodsService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 替换商品信息 Controller
 * </p>
 *
 * @author rfwang
 * @since 2021-08-05
 */
@Api(tags = "替换商品信息")
@RestController
@RequestMapping(value="/omsReplacedGoods", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsReplacedGoodsController extends BaseController {
    @Autowired
    private IOmsReplacedGoodsService omsReplacedGoodsService;

    /**
     * 获取 OMS替换商品记录 列表
     *
     * @param omsReplacedGoodsDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 OMS替换商品记录 列表", notes = "获取 OMS替换商品记录 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsReplacedGoodsVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(OmsReplacedGoodsDTO omsReplacedGoodsDTO, HttpServletRequest request) {
        try {
            omsReplacedGoodsDTO.setShipmentCode(UserUtil.getLoginWarehouseCodeOrShipmentCode());
            OmsReplacedGoodsVO omsReplacedGoodsVOSum = omsReplacedGoodsService.queryListSum(omsReplacedGoodsDTO);
            List<OmsReplacedGoodsVO> omsReplacedGoodsVOList = omsReplacedGoodsService.queryList(omsReplacedGoodsDTO);

            Page returnPage = new Page(omsReplacedGoodsDTO.getPageNum(), omsReplacedGoodsDTO.getPageSize());
            returnPage.setRecords(omsReplacedGoodsVOList);
            returnPage.setTotal(omsReplacedGoodsVOSum.getTotal());
            returnPage.setSumObject(omsReplacedGoodsVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * OMS替换商品
     *
     * @param omsReplacedGoodsDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "OMS替换商品", notes = "OMS替换商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "shipmentCode", value = "货主code", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "shopCode", value = "店铺code", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "warehouseCode", value = "仓库code", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "originalGoodsCode", value = "原始商品code", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "originalGoodCode", value = "原始商品编码", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "originalGoodName", value = "原始商品名称", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "replacedGoodsCode", value = "替换商品code", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "replacedGoodCode", value = "替换商品编码", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "replacedGoodName", value = "替换商品名称", paramType = "body", dataType = "string", required = true),
            @ApiImplicitParam(name = "saleCodes", value = "oms销售单号集合", paramType = "body", dataType = "string", required = true),
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsReplacedGoodsVO.class))
    @NeedLogin
    @PutMapping()
    public Result replaceGoods(@RequestBody OmsReplacedGoodsDTO omsReplacedGoodsDTO, HttpServletRequest request) {
        try {
            return omsReplacedGoodsService.replaceGoods(omsReplacedGoodsDTO);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

 }
